package com.zyq.ssyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyq.ssyx.acl.service.RoleService;
import com.zyq.ssyx.common.result.CommonResponse;
import com.zyq.ssyx.model.acl.Role;
import com.zyq.ssyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author clap of thunder
 * @date 2023-11-23 17:23
 * @description
 */

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/acl/role")
@CrossOrigin
public class RoleController {

    @Autowired
    RoleService roleService;

    /**获取角色分页列表(带搜索)*/
    @ApiOperation(value = "获取角色分页列表(带搜索)")
    @GetMapping("{page}/{limit}")
    public CommonResponse getPageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "roleQueryVo", value = "查询对象", required = false)
                    RoleQueryVo roleQueryVo){//入参VO是为了接值用作后续的模糊查询

        Page<Role> pageParam = new Page<>(page,limit);//分页插件组件
        IPage<Role> pageModel= roleService.selectPage(pageParam,roleQueryVo);
        return CommonResponse.ok(pageModel);

    }
    /**保存一个新角色*/
    @ApiOperation("保存一个新角色")
    @PostMapping("/save")
    public CommonResponse save(@RequestBody Role role){
        boolean save = roleService.save(role);
        if (save){
            return CommonResponse.ok(null);
        }else {
            return CommonResponse.fail(null);
        }
    }
    /**获取某个角色*/
    @ApiOperation("获取某个角色")
    @GetMapping("/get/{id}")
    public CommonResponse getById(@PathVariable Long id){
        Role roleSelectById = roleService.getById(id);
        return CommonResponse.ok(roleSelectById);
    }
    /**更新一个角色*/
    @ApiOperation("更新一个角色")
    @PutMapping("/update")
    public CommonResponse updateById(@RequestBody Role role){
        boolean update = roleService.updateById(role);
        if (update){
            return CommonResponse.ok(null);
        }else {
            return CommonResponse.fail(null);
        }
    }
    /**删除一个角色*/
    @ApiOperation("删除一个角色")
    @DeleteMapping("/remove/{id}")
    public CommonResponse removeById(@PathVariable Long id){
        boolean remove = roleService.removeById(id);
        if (remove){
            return CommonResponse.ok(null);
        }else {
            return CommonResponse.fail(null);
        }
    }
    /**批量删除多个角色*/
    @ApiOperation("批量删除多个角色")
    @DeleteMapping("/batchRemove")
    public CommonResponse removeRoles(@RequestBody List<Long> ids){
        boolean remove = roleService.removeByIds(ids);
        if (remove){
            return CommonResponse.ok(null);
        }else {
            return CommonResponse.fail(null);
        }
    }

}
